//
//  CPYSingerInformationViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/4/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYSingerInformationViewController.h"
#import "CPYSingerInformationModel.h"
#import "CPYSingerInformationTableViewCell.h"
#import "CPYSONGSModel.h"
#import "CPYDiscoveryPageSongListModel.h"
@interface CPYSingerInformationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) TTMusicBaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIImageView *titleImage;
@property (nonatomic, strong) CPYSONGSModel *songsModel;

@end

@implementation CPYSingerInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self creatTableView];
    [self creatData];
    [self creatNavButtonItem];
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//http://api.dongting.com/song/singer/%@/songs?page=%ld&size=50&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88265664085061&longtitude=121.5391135272595

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    NSLog(@"%f", _tableView.contentOffset.y);
    CGFloat y = _tableView.contentOffset.y;
    if (y < -ImageHeight) {
        CGRect frame = _titleImage.frame;
        frame.origin.y = y;
        frame.size.height = -y * FitHeight;
        _titleImage.frame = frame;
    }
    return;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)creatData {
    [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88265664085061&longtitude=121.5391135272595", self.singerID] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:result];

        for (NSDictionary *dic in result[@"data"]) {
            CPYSingerInformationModel *singerModel = [[CPYSingerInformationModel alloc] init];
            [singerModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:singerModel];
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SHORTHEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.contentInset = UIEdgeInsetsMake(ImageHeight * FitHeight, 0, 0, 0);
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -ImageHeight * FitHeight - 30, self.view.frame.size.width, ImageHeight * FitHeight)];
//    [_titleImage sd_setImageWithURL:[NSURL URLWithString:self.imageStr]];
//    _titleImage.userInteractionEnabled = YES;
//    _titleImage.contentMode = UIViewContentModeScaleAspectFill;
//    [_tableView addSubview:_titleImage];
    [_tableView registerClass:[CPYSingerInformationTableViewCell class] forCellReuseIdentifier:@"CPYSingerInformationTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dataSource.count != 0) {
        return _dataSource.count;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataSource.count != 0) {
        CPYSingerInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYSingerInformationTableViewCellIdentifier"];
        CPYSingerInformationModel *singerModel = _dataSource[indexPath.row];
        [cell.singerImageView sd_setImageWithURL:[NSURL URLWithString:self.imageStr]];
        cell.singerinformationModel = singerModel;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80 * FitHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.row;
    [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:self.songsModel];
    [TTMusicPlayBottomView playBottomView].songsModel = _songsModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
